<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="产废单位名称:">
          <el-input v-model="formInline.wpcName" clearable />
        </el-form-item>
        <el-form-item label="废物名称:">
          <el-input v-model="formInline.name" clearable />
        </el-form-item>
        <el-form-item label="废物代码:">
          <el-input v-model="formInline.codeContent" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button icon="el-icon-arrow-left" @click="cancel">
            返 回
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" :disabled="disabled" @click="selected">
            选 择
          </el-button>
        </el-form-item>
        <el-form-item>
          <div> 当前页统计： 容器总数量：{{ summaryWastCount }} 个 | 废物总重量：
            {{ summaryWastWeight }} 吨
          </div>
        </el-form-item>
      </el-form>
      <div class="result" style="margin-top: -10px;">
        <el-table ref="multipleTable" v-loading="loading" border :data="tableData" tooltip-effect="dark"
                  style="width: 100%" @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="name" label="废物名称" />
          <el-table-column prop="number" align="center" label="数字识别码" />
          <el-table-column prop="categoryContent" align="center" label="废物类别" />
          <el-table-column prop="codeContent" align="center" label="废物代码" />
          <el-table-column prop="pos" align="center" label="存放位置" />
          <el-table-column prop="hazardousSubstancesName" align="center" label="危害成分" />
          <el-table-column prop="orgName" align="center" label="产废单位" width="200" />
          <el-table-column prop="inAmount" align="center" label="重量(吨)" />

          <el-table-column prop="characteristicContent" align="center" label="危险特性" />
          <el-table-column prop="containerCategory" align="center" label="容器类型" width="120">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark">
                {{ scope.row.containerCategory | parseContainerTypeType }}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column prop="physicalProperties" align="center" label="废物形态" width="120">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark">
                {{ scope.row.physicalProperties | parsePhysicalPropertiesType }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="describe" label="备注" align="center" />

        </el-table>

        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next,sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />
        </div>
      </div>

    </div>
  </div>
</template>

<script>
// Table组件相关逻辑，需要引用
import {
  pageMixin
} from "../../../mixins/index";
import {
  transportApplyState,
  PhysicalProperties,
  ContainerType
} from "../../../utils/resource.js";
import {
  getWastObjListByCode,
} from '../../../api/warehouse.js';
import {
  getFloat
} from '../../../utils/index.js';
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },
    parseType(value) {
      if (value != null) {
        return transportApplyState.find((p) => p.value === `${value}`).label;
      }
    },
    parsePhysicalPropertiesType(value) {
      if (value != null) {
        return PhysicalProperties.find((p) => p.value === `${value}`).label;
      }
    },
    parseContainerTypeType(value) {
      if (value != null) {
        return ContainerType.find((p) => p.value === `${value}`).label;
      }
    },

  },
  mixins: [pageMixin],

  data() {
    return {
      transportApplyStateOption: transportApplyState,
      formInline: {
        name: "",
        codeContent: "",
        wpcName: "",
      },
      tempOutHouseObjInfo_ByUse: {},
    };
  },
  async created() {
    await this.init();
  },
  methods: {
    async init() {
      this.tempOutHouseObjInfo_ByUse = JSON.parse(localStorage.getItem('tempOutHouseObjInfo_ByUse'));
      if (!this.tempOutHouseObjInfo_ByUse) {
        this.tempOutHouseObjInfo_ByUse.wastObj = {};
      }

      this.tableData = [];
      this.loading = true;

      const {
        data,
        count
      } = await getWastObjListByCode({
        limit: this.pageSize,
        page: this.currentPage,
        searchParam: JSON.stringify({
          Name: this.formInline.name,
          CodeContent: this.formInline.codeContent,
          WpcName: this.formInline.wpcName,
          Status: "10"
        })
      });
      data.forEach(item => {
        item.pos = (item.wareHouseName === null ? "" : item.wareHouseName + "-") + (item
          .wareHouseLocationName === null ? "" : item.wareHouseLocationName)
      })
      this.tableData = data;
      this.undataSummaryData(data);
      this.total = count;
      this.loading = false;
    },

    async search() {
      await this.init();
    },
    async reset() {
      this.formInline = {
        name: "",
        codeContent: "",
        wpcName: ""
      };
      await this.init();
    },
    undataSummaryData(tableDataList) {
      this.summaryWastCount = 0;
      this.summaryWastWeight = 0;

      tableDataList.forEach(item => {
        if (item.inAmount < 0) {
          item.inAmount = 0
        }
        this.summaryWastWeight += getFloat(item.inAmount, 5);
        this.summaryWastCount += 1;
      })
    },
    selected() {
      var obj = this.tableData.find((p) => p.id === this.id);
      this.tempOutHouseObjInfo_ByUse.wastObj = obj; // 选中的一行数据
      this.tempOutHouseObjInfo_ByUse.wastId = obj.id;

      localStorage.setItem('tempOutHouseObjInfo_ByUse', JSON.stringify(this.tempOutHouseObjInfo_ByUse));
      this.$router.push("/warehouse/outhouse_ByUse");
    },
    cancel() {
      this.$router.push("/warehouse/outhouse_ByUse");
    },

  },
};
</script>

<style lang="scss" scoped>
  .result {
    margin-top: 10px;
  }

  .page {
    margin-top: 20px;
    text-align: right;
    margin-bottom: 40px;
  }

  .show-result {
    margin-top: 10px;
  }
</style>
